package com.zrz.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import org.apache.commons.lang.StringUtils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * 工具
 * @author wangshuxiong
 * @since 2017/02/08  14:16
 */
public final class ToolUtils {
    private static final int VERIFY_CODE_LENGTH = 6;
    private static final int VERIFY_CODE_BOUND = 10;
    private static final String MOBILE = "^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\d{8}$";
    //密码字符要求为密码由6-18位,由数字、字母或者特殊字符中至少两种组成
    private static final String PASSWORD_PATTERN = "(?!^\\d+$)(?!^[a-zA-Z]+$)(?!^[!@#$%^&*()]+$).{6,16}";


    /**
     * like 两边匹配
     * @author wangshuxiong
     * @since 2017/2/21 18:23
     * @param query 模糊匹配的value
     * @return 处理后的值
     */
    public static final String likeBoth(String query) {
        if (StringUtils.isBlank(query)) {
            query = StringUtils.EMPTY;
        }
        return String.format("%%%s%%", query);
    }


    /**
     * 不保留小数，向下取整
     * @param value 值
     * @return 取整后的值
     */
    public static String point(BigDecimal value) {
        if (value == null) {
            value = BigDecimal.ZERO;
        }
        BigDecimal afterValue = value.setScale(0,  BigDecimal.ROUND_FLOOR);
        return pointByScale(afterValue,0);
    }

    /**
     * 截断小数
     * @param value 数字
     * @param scale 截断小数精度
     * @return 字符串
     */
    private static String pointByScale(BigDecimal value, int scale) {
        return String.format("%." + scale + "f", value);
    }




    public static String generateVerifyCode() {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();

        for (int i = 0; i < VERIFY_CODE_LENGTH; i++) {
            sb.append(random.nextInt(VERIFY_CODE_BOUND));
        }

        return sb.toString();
    }

    /**
     * map 转对象
     * @author wangshuxiong
     * @since 2017/3/27 16:39
     * @param
     * @return
     */
    public static <T> T mapToObject(Map<String, Object> map, Class<? extends T> beanClass)  {
        if (map == null)
            return null;
        try {
            Object obj = beanClass.newInstance();
            org.apache.commons.beanutils.BeanUtils.populate(obj, map);
            return (T)obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 对象转map
     * @author wangshuxiong
     * @since 2017/3/27 16:39
     * @param
     * @return
     */
    public static Map objectToMap(Object obj){
       return new org.apache.commons.beanutils.BeanMap(obj);
    }



    public static HashMap<String, String> xml2Map(String source) {
        HashMap<String, String> map = Maps.newHashMap();
        try {
            Document document = DocumentHelper.parseText(source);
            Element element = document.getRootElement();
            List<Element> elements = element.elements();
            elements.stream().forEach(item -> {
                map.put(item.getName(), item.getText());
            });
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return map;
    }


    public static final String randomStr(int length) {
        if (length == 0) {
            return "";
        }
        int count = length / 32 + 1;
        StringBuilder randomPool = new StringBuilder();
        for (int i = 0; i < count; i++) {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            randomPool.append(uuid);
        }
        return StringUtils.left(randomPool.toString(), length);
    }

    public static List<String> str2List(String source){
        ObjectMapper mapper = new ObjectMapper();
        try {
            return  mapper.readValue(source,List.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Lists.newArrayList();
    }


}
